package com.wenx.camel;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.RouteDefinition;

import static org.apache.camel.builder.Builder.constant;
import static org.apache.camel.builder.Builder.header;

public class DemoSelect {
    public static void main(String[] args) throws Exception {
        ModelCamelContext camelContext = new DefaultCamelContext();
        RouteDefinition routeDefinition = new RouteDefinition();
        routeDefinition.from("timer:foo?repeatCount=1")
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        exchange.getOut().setBody("ab");
                        exchange.getOut().setHeader("aa",true);
                    }
                })
                .choice()
                    .when(header("aa").isEqualTo(true))
                    .setBody(constant("aaa"))
                    .process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            System.out.println("exchange = " + exchange.getIn().getBody());
                        }
                    })
                    .otherwise()
                    .setBody(constant("bbb"))
                    .process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            System.out.println("exchange = " + exchange.getIn().getBody());
                        }
                    })
                    .endChoice()
                    .end()
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("exchange = " + exchange.getIn().getBody());
                    }
                });
        camelContext.addRouteDefinition(routeDefinition);
        camelContext.start();
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        camelContext.stop(); // 最后停止CamelContext
    }
}
